/*******************************************************************************
 * tuannh ********* function : load layer list
 ******************************************************************************/
// init parameter
var _divBase = null;
var _divWMS = null;
var _divMFS = null;

// var _divLayers_Feature = _getElementById('divLayers_Feature');
var _select_Tool_cboLayers = null;
var _div_Tool_divSelect_Tool = null;

var _legend_LayerList_Base = null;
var _legend_LayerList_WMS = null;
var _legend_LayerList_WFS = null;

var arr_WMS = null;
var arr_WFS = null;

/** ************************************************** */
/** ************* PORTLET LAYERS ********************* */
/** ************************************************** */
function init_LayerList() {
	// alert('Init LayerList');
	_divBase = $('#divBase');
	_divWMS = $('#divWMS');
	_divMFS = $('#divMFS');
	arr_WMS = new Array();
	_legend_LayerList_Base = _getElementById('legend_LayerList_Base');
	_legend_LayerList_Base.innerHTML = l_legend_LayerList_Base;
	_legend_LayerList_Base.style.cursor = "pointer";
	_legend_LayerList_Base.onclick = showhideLayer;
	_legend_LayerList_Base.divID = _divBase;
	
	_legend_LayerList_WMS = _getElementById('legend_LayerList_WMS');
	_legend_LayerList_WMS.innerHTML = l_legend_LayerList_WMS;
	_legend_LayerList_WMS.style.cursor = "pointer";
	_legend_LayerList_WMS.onclick = showhideLayer;
	_legend_LayerList_WMS.divID = _divWMS;
	
	_legend_LayerList_WFS = _getElementById('legend_LayerList_WFS');
	_legend_LayerList_WFS.innerHTML = l_legend_LayerList_WFS;
	_legend_LayerList_WFS.style.cursor = "pointer";
	_legend_LayerList_WFS.onclick = showhideLayer;
	_legend_LayerList_WFS.divID = _divMFS;

	var i = 0;
	var map = null;
	// WMS
	var objLabel = null;
	var objCheckbox = null;
	var objBR = null;

	for (i = 0; i < map_WMS.length; i++) {
		// IndexLayer | LayerId | Layer_Name | Visible | StyleMap | Projection |
		// SaveObj | Version | URL | Feature_Type | Feature_NS |
		// Geometry_Name | Geometry_Type | DisplayClass_Draw |
		// DisplayClass_Modify | DisplayClass_Del | Map_Service | Base_Map | Display
		map = map_WMS[i];
		if(!map.Display) continue;
		objLabel = document.createElement('label');

		objCheckbox = document.createElement('input');
		objCheckbox.type = 'checkbox';
		objCheckbox.id = 'chkLayer_id_' + map.LayerId;
		objCheckbox.name = 'chkLayer_name_' + map.LayerId;
		objCheckbox.checked = map.Visible;
		objCheckbox.defaultChecked = map.Visible;

		objLabel.Map_Service = map.Map_Service;
		objLabel.LayerID = map.LayerId;
		objLabel.IndexLayer = map.IndexLayer;
		objLabel.Visible = map.Visible;
		objLabel.className = "sizeTool";
		objLabel.appendChild(objCheckbox);
		objLabel.onmousedown = Visible_Layer_TabControl;
		// objCheckbox.onmousedown = Visible_Layer;
		if (map.Visible)
			arr_WMS[arr_WMS.length] = map.LayerId;
		objLabel.innerHTML += "   " + map.Layer_Name;
		_divWMS.append(objLabel);

		objBR = document.createElement('br');
		_divWMS.append(objBR);
	}

	arr_WFS = new Array();
	// WFS
	for (i = 0; i < map_WFS.length; i++) {
		// IndexLayer | LayerId | Layer_Name | Visible | StyleMap | Projection |
		// SaveObj | Version | URL | Feature_Type | Feature_NS |
		// Geometry_Name | Geometry_Type | DisplayClass_Draw |
		// DisplayClass_Modify | DisplayClass_Del | Map_Service | Base_Map | Display
		map = map_WFS[i];
		if(!map.Display) continue;
		objLabel = document.createElement('label');

		objCheckbox = document.createElement('input');
		objCheckbox.type = 'checkbox';
		objCheckbox.id = 'chkLayer_' + map.LayerId;
		objCheckbox.name = 'chkLayer_' + map.LayerId;

		objLabel.id = "lblLayer_" + map.LayerId;
		objLabel.Map_Service = map.Map_Service;
		objLabel.LayerID = map.LayerId;
		objLabel.htmlFor = objCheckbox.id;
		objLabel.appendChild(objCheckbox);
		objLabel.IndexLayer = map.IndexLayer;
		objLabel.Visible = map.Visible;
		objLabel.className = "sizeTool";
		objCheckbox.checked = map.Visible;
		objCheckbox.defaultChecked = map.Visible;

		objLabel.onmousedown = Visible_Layer_TabControl;
		// objCheckbox.onmousedown = Visible_Layer;
		if (map.Visible)
			arr_WFS[arr_WFS.length] = map.LayerId;
		objLabel.innerHTML += "   " + map.Layer_Name;
	    /*
		 * $("#" + objLabel.id).contextMenu( { menu: 'myMenu' }, function
		 * (action, el, pos) { alert( 'Action: ' + action + '\n\n' + 'Element
		 * ID: ' + $(el).attr('id') + '\n\n' + 'X: ' + pos.x + ' Y: ' + pos.y + '
		 * (relative to element)\n\n' + 'X: ' + pos.docX + ' Y: ' + pos.docY + '
		 * (relative to document)' ); } );
		 */
		_divMFS.append(objLabel);

		objBR = document.createElement('br');
		_divMFS.append(objBR);
	}
}

function showhideLayer()
{
	var _divID = this.divID;
	if(_divID==null) return;
	if(_divID.css('display') == "none")
	{
		$(_divID).show(500);
	}
	else
	{
		$(_divID).hide(500);
	}
}

function Visible_Layer_TabControl(e) {
	// right click
	
	  if(e.buttons == 2) 
	  {
		  var x; var y; if (e.pageX || e.pageY) { x = e.pageX;
		  y = e.pageY; } else { x = e.clientX + document.body.scrollLeft +
		  document.documentElement.scrollLeft; y = e.clientY +
		  document.body.scrollTop + document.documentElement.scrollTop; }
		  _div_Map_ContextMenuLayer.oncontextmenu = false;
		  _div_Map_ContextMenuLayer.style.left = x + "px";
		  _div_Map_ContextMenuLayer.style.top = y + "px"; return false; 
	  }
	 
	// left click
	// alert(l.abc);
	var indexLayer = this.IndexLayer;
	if (!this.Visible)
		map.layers[indexLayer].setVisibility(true);
	else
		map.layers[indexLayer].setVisibility(false);
	this.Visible = !this.Visible;
}

function Visible_Layer() {
	var bMapService = this.Map_Service;
	var _LayerID = this.LayerID;
	var objChk_Layer = this.firstChild;
	if (objChk_Layer.checked) {
		if (bMapService)
			removeLayerID(_LayerID, arr_WMS);
		else
			removeLayerID(_LayerID, arr_WFS);
	} else {
		if (bMapService)
			addLayerID(_LayerID, arr_WMS);
		else
			addLayerID(_LayerID, arr_WFS);
	}
}

function removeLayerID(layerID, arrWebServices) {
	var i = 0;
	var sLayerID = '';
	for (i = 0; i < arrWebServices.length; i++) {
		sLayerID = arrWebServices[i];
		if (sLayerID == layerID) {
			sLayerID = arrWebServices[arrWebServices.length - 1];
			arrWebServices[arrWebServices.length - 1] = layerID;
			arrWebServices[i] = sLayerID;
			break;
		}
	}
	arrWebServices.pop();
}

function addLayerID(layerID, arrWebServices) {
	var i = 0;
	var sLayerID = '';
	var bAdd = true;
	for (i = 0; i < arrWebServices.length; i++) {
		sLayerID = arrWebServices[i];
		if (sLayerID == layerID) {
			bAdd = false;
			break;
		}
	}
	if (bAdd)
		arrWebServices.push(layerID);
}

function get_layer() {
	var URL = '';
	var i = 0;
	for (i = 0; i < arr_WMS.length; i++) {
		URL += arr_WMS[i] + ', ';
	}
	for (i = 0; i < arr_WFS.length; i++) {
		URL += arr_WFS[i] + ', ';
	}

	URL = URL.substring(0, URL.length - 2);
	alert(URL);
}
/** ***** end function ******* */

/*******************************************************************************
 * tuannh ********* function : tools display
 ******************************************************************************/
function init_Tools() {
	// _divLayers_Feature = document.getElementById('divLayers_Feature');
	_select_Tool_cboLayers = _getElementById('select_Tool_cboLayers');
	_div_Tool_divSelect_Tool = _getElementById('div_Tool_divSelect_Tool');

	_getElementById('legend_Tool_divLayersList').innerHTML = l_legend_Tool_divLayersList;
	_getElementById('td_Tool_Layer').innerHTML = l_td_Tool_Layer;
	_getElementById('legend_Tool_divTools').innerHTML = l_legend_Tool_divTools;
	_getElementById('option_Tool_select_0').innerHTML = l_option_Tool_select_0;

	var i = 0;
	// WFS
	var map = null;
	for (i = 0; i < map_WFS.length; i++) {
		// IndexLayer | LayerId | Layer_Name | Visible | StyleMap | Projection |
		// SaveObj | Version | URL | Feature_Type | Feature_NS |
		// Geometry_Name | Geometry_Type | DisplayClass_Draw |
		// DisplayClass_Modify | DisplayClass_Del | Map_Service | Base_Map
		map = map_WFS[i];
		if(!map.Display) continue;
		var elOptNew = document.createElement('option');
		elOptNew.text = map.Layer_Name;
		elOptNew.value = map.IndexLayer;
		try {
			// not IE
			_select_Tool_cboLayers.add(elOptNew, null);
		} catch (ex) {
			// IE only
			_select_Tool_cboLayers.add(elOptNew);
		}
	}
	// Status_LayerFeature(_divLayers_Feature, true);

	// menu tools
	// <table>
	// <tr>
	// <td>
	// <label for="chkDraw_Line_Angle_Length">
	// +
	// <input type="checkbox" value="chkDraw_Line_Angle_Length"
	// id="chkDraw_Line_Angle_Length" name="chkDraw_Line_Angle_Length" />
	// V? line theo g�c & ?? d�i cho tr??c
	// </label>
	// </td>
	// </tr>
	// </table>
	var objTable = null;
	var objTR = null;
	var objTD = null;
	var objLabel = null;
	var objInput = null;
	var menu_Item = null;

	objTable = document.createElement('table');
	for (i = 0; i < menu_WS.length; i++) {
		// ID|Name|Level|Group|Child|Child_ID|Height
		menu_Item = menu_WS[i];
		objTR = document.createElement('tr');
		objTD = document.createElement('td');
		objLabel = document.createElement('label');
		objLabel.innerHTML += '+';
		objInput = document.createElement('input');
		objInput.type = 'checkbox';
		objInput.value = 'chk_' + menu_Item.ID;
		objInput.name = 'chk_' + menu_Item.ID;
		objInput.id = 'chk_' + menu_Item.ID;
		objLabel.className = "sizeTool";
		objLabel.appendChild(objInput);
		objLabel.innerHTML += "   " + menu_Item.Name;

		objTD.appendChild(objLabel);
		objTR.appendChild(objTD);
		objTable.appendChild(objTR);
	}
	_div_Tool_divSelect_Tool.appendChild(objTable);
}

function getTextComboSS(str)
{
	 alert("name = "+str);
	 SELECTED_LAYER_NAME = str;
	 for (var i=0 ;i<map.layers.length;i++)
 	{
 	  if(map.layers[i].name == SELECTED_LAYER_NAME)
 		  {
 		  	ID_LAYER = i;    
 		  	LAYER_LENGTH = map.layers[ID_LAYER].features.length;
 		  	ARR_LAYER = new Array();
 		  	break;
 		  }
 	}    
	 alert("OK");
}
var indexLayerPre = -1;
var bSelectIndexPre = false;
function Zoom_To_Layer() {
	var _index = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var vectorObj = map.layers[_index];
	Set_Tooltip_Feature(vectorObj);
	if(bSelectIndexPre) return;
	if(!Portlet_Panel.setCurrent_ImgAction(panelPortlet, panelPortlet.current_ImgAction_Deault))
	{
		bSelectIndexPre = true;
		if(indexLayerPre!=-1) _select_Tool_cboLayers.selectedIndex = indexLayerPre;
		bSelectIndexPre = false;
		return false;
	}
	if(!bSaveEditting)
	{
		bSelectIndexPre = true;
		if(indexLayerPre!=-1) _select_Tool_cboLayers.selectedIndex = indexLayerPre;
		alert("Bạn cần phải lưu dữ liệu");
		bSelectIndexPre = false;
		return false;
	}
	bSelectIndexPre = false;
	indexLayerPre = _select_Tool_cboLayers.selectedIndex;
	var i = 0, j = 0;
	var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	
	arrField_Alias_Current = new Array();
	if (panelPortlet.current_ImgAction != null) {
		if (panelPortlet.current_ImgAction.divContainer != null)
			panelPortlet.current_ImgAction.divContainer.style.display = "none";
	}
	if (indexLayer == 0) {
		// Portlet_Panel.hidePanel(panelPortlet.divControl);
		for(i=0;i<Potrlet_Panel_Collection.length;i++)
		{
			var objPanel = Potrlet_Panel_Collection[i];
			var len_ImgPortlet = objPanel.imageList_Ctrl.length;
			for (j = 0; j < len_ImgPortlet; j++) {
				var objPortlet_Img = objPanel.imageList_Ctrl[j];
				if(!objPortlet_Img.Tool_Base) objPortlet_Img.imgControl.LI.style.display = "none";
			}
		}		
		Portlet_Panel.hidePanel(panel_line_extension_Portlet.divControl);
		layer_Selected = null;
		getAttr("_div_Map_divToolbar", "top:" + (map_Top + 5) + "px; left:" + (map_Left + map_Width - _div_Map_divToolbar.offsetWidth) +  "px; " 
				+ "width: auto; height: auto; display: block");
		return false;
	}
	// alert("map.layers[indexLayer]: " +
	// map.layers[indexLayer].features.length);
	if (!map.layers[indexLayer].visibility) {
		alert(l_msg_Tool_ZoomtoLayer);
		_select_Tool_cboLayers.selectedIndex = 0;
		// Portlet_Panel.hidePanel(panelPortlet.divControl);
		for(i=0;i<Potrlet_Panel_Collection.length;i++)
		{
			var objPanel = Potrlet_Panel_Collection[i];
			var len_ImgPortlet = objPanel.imageList_Ctrl.length;
			for (var j = 0; j < len_ImgPortlet; j++) {
				var objPortlet_Img = objPanel.imageList_Ctrl[j];
				if(!objPortlet_Img.Tool_Base) objPortlet_Img.imgControl.LI.style.display = "none";
			}
		}
		Portlet_Panel.hidePanel(panel_line_extension_Portlet.divControl);
		layer_Selected = null;
		getAttr("_div_Map_divToolbar", "top:" + (map_Top + 5) + "px; left:" + (map_Left + map_Width - _div_Map_divToolbar.offsetWidth) +  "px; " 
				+ "width: auto; height: auto; display: block");
		return false;
	}
	
	Portlet_Panel.visiblePanel(panelPortlet.divControl);
	layer_Selected = Vector_Array[indexLayer - numBaseLayer];
	var geom_Type = layer_Selected.Geometry_Type;
	var iPortletImg_Count = 0;
	var width_Img = 0;
	for(i=0;i<Potrlet_Panel_Collection.length;i++)
	{
		var objPanel = Potrlet_Panel_Collection[i];
		var len_ImgPortlet = objPanel.imageList_Ctrl.length;
		for (j = 0; j < len_ImgPortlet; j++) {
			var objPortlet_Img = objPanel.imageList_Ctrl[j];
			if(!objPortlet_Img.Tool_Base) 
			{
				objPortlet_Img.imgControl.LI.style.display = "block";
				// neu la panel toolbar
				if(objPanel.p_index == 0) iPortletImg_Count++;
				width_Img = objPortlet_Img.width_Img;
			}
		}
	}
	if(panel_line_extension_Portlet!= null)
	{
		var len_ImgPortlet = panel_line_extension_Portlet.imageList_Ctrl.length;
		switch (geom_Type) {
		case "point":
			Portlet_Panel.hidePanel(panel_line_extension_Portlet.divControl);
			break;
		case "polygon":
			for (i = 0; i < len_ImgPortlet; i++) {
				var objPortlet_Img = panel_line_extension_Portlet.imageList_Ctrl[i];
				if (!objPortlet_Img.Line_Tool_Extension && !objPortlet_Img.Polygon_Tool_Extension)
					objPortlet_Img.imgControl.LI.style.display = "none";
				else if (!objPortlet_Img.Polygon_Tool_Extension)
					objPortlet_Img.imgControl.LI.style.display = "none";
				else objPortlet_Img.imgControl.LI.style.display = "block";
			}
			Portlet_Panel.visiblePanel(panel_line_extension_Portlet.divControl);
			getAttr(
					"_div_Map_divToolbar_LineExtension",
					"top:"
							+ (map_Top + panelPortlet.divControl.offsetHeight + 5)
							+ "px; left:"
							+ (map_Left + map_Width - panel_line_extension_Portlet.divControl.offsetWidth)
							+ "px; " + "width: auto; height: auto; display: block");
			break;
		case "polyline":
			for (i = 0; i < len_ImgPortlet; i++) {
				var objPortlet_Img = panel_line_extension_Portlet.imageList_Ctrl[i];
				if (!objPortlet_Img.Line_Tool_Extension && !objPortlet_Img.Polygon_Tool_Extension)
					objPortlet_Img.imgControl.LI.style.display = "none";
				else if (!objPortlet_Img.Line_Tool_Extension)
					objPortlet_Img.imgControl.LI.style.display = "none";
				else objPortlet_Img.imgControl.LI.style.display = "block";
			}
			Portlet_Panel.visiblePanel(panel_line_extension_Portlet.divControl);
			getAttr(
					"_div_Map_divToolbar_LineExtension",
					"top:"
							+ (map_Top + panelPortlet.divControl.offsetHeight + 5)
							+ "px; left:"
							+ (map_Left + map_Width - panel_line_extension_Portlet.divControl.offsetWidth)
							+ "px; " + "width: auto; height: auto; display: block");
			break;
		default:
			break;
		}
	}
	// tru di (iPortletImg_Count*width_Img): so luong portlet image xuat hien
	// them tren panel
	// tru di (iPortletImg_Count*1): do rong cua padding trog CSS cua the LI
	_div_Map_divToolbar.style.width = (_div_Map_divToolbar.offsetWidth + iPortletImg_Count*width_Img + iPortletImg_Count*1) + "px";
	getAttr("_div_Map_divToolbar", "top:" + (map_Top + 5) + "px; left:" + (map_Left + map_Width - _div_Map_divToolbar.offsetWidth) +  "px; " 
			+ "width: auto; height: auto; display: block");
	getAttr("_div_Map_divToolbar_LineExtension", "top:" + (map_Top + _div_Map_divToolbar.offsetHeight + 5) + "px; left:" 
			+ (map_Left + map_Width - _div_Map_divToolbar_LineExtension.offsetWidth) +  "px; " + "width: auto; height: auto");
	var extent = map.layers[indexLayer].getDataExtent();
	if (extent == null) {

	} else
		map.zoomToExtent(extent);
	setTimeout("setWidth_Toolbar();", 10);
	feature_Current_Action = null;
	
	// nguyennv
	str = Vector_Array[indexLayer - numBaseLayer].LayerId;
	DISPLAY_LAYER_NAME = Vector_Array[indexLayer - numBaseLayer].Layer_Name;
	getTextCombo(indexLayer,str);
	
	ID_LAYER = indexLayer;
	LAYER_LENGTH = map.layers[ID_LAYER].features.length;
	// nguyennv
	return false;
}

function setWidth_Toolbar() {
	getAttr("_div_Map_divToolbar", "left:"
			+ (map_Left + map_Width - _div_Map_divToolbar.offsetWidth) + "px");
}

function Status_LayerFeature(el, bDisabled) {
	try {
		el.disabled = bDisabled;
	} catch (ex) {
	}
	if (el.childNodes) {
		if (el.childNodes.length > 0) {
			for ( var i = 0; i < el.childNodes.length; i++) {
				Status_LayerFeature(el.childNodes[i], bDisabled);
			}
		}
	}
}

function toogle_Layer_Feature() {
	if (_select_Tool_cboLayers.selectedIndex == 0) {
		Status_LayerFeature(_divLayers_Feature, true);
	} else {
		Status_LayerFeature(_divLayers_Feature, false);
	}
	return false;
}

function get_tools() {

}

function refreshPortlet() {
	alert(themeDisplay);
	var plid = themeDisplay.getPlid();
	alert(plid);
	var nsShort = ("<portlet:namespace/>".substr(1)).substr(0,
			"<portlet:namespace/>".length - 2);
	alert(nsShort);
	var portletId = "#p_p_id<portlet:namespace/>";
	var loadId = "p_load<portlet:namespace/>";
	url = "/c/portal/render_portlet?p_l_id=" + plid + "&p_p_id=" + nsShort
			+ "&p_p_action=0&p_p_state=normal&p_p_mode=view";
	alert(url);
	jQuery(portletId).empty().append(
			'<div class="loading-animation" id="' + loadId + '"></div>');
	jQuery(portletId).load(url);
}
/** ******* end function ********** */